#include <cstddef>
#include <iostream>
template <typename T>
class Vector
{
    T* buffer_;
    size_t size_;
public:
    explicit Vector(size_t n):size_(n){
        buffer_ = new T[n];
    }
    ~Vector(){
        delete[] buffer_;
    }
    T& operator[](size_t index){
        return buffer_[index];
    }
    size_t size(){
        return size_;
    }
};
template<typename T> class List_itor;
template<typename T>
class List
{
public:
    friend class List_itor<T>;
    struct Link
    {
        T* data;
        Link* next;
    };

    List(){
        head_ = new Link();
        head_->next = head_;
        tail_ = head_;
    }
    void put(T* data){
        Link* pnode = new Link();
        pnode->data = data;
        pnode->next = NULL;
        tail_->next = pnode;
        tail_ = pnode;
    }
    T* get(){
        return tail_->data;
    }
private:
    Link *head_, *tail_;
};
template< typename T>
class Itor
{
public:
    virtual T* first() = 0;
    virtual T* next() = 0;
};
template<typename T> class Vector_itor
    :public Itor<T>
{
    Vector<T> &v_;
    size_t index_;
public:
    Vector_itor(Vector<T>& v):v_(v),index_(0){
    }
    T* first()
    {
        return (v_.size())? &v_[index_=0]:0;
    }
    T* next(){
        return (++index_ < v_.size()) ?
            &v_[index_]:0;
    }
};
template<typename T>
class List_itor:public Itor<T>
{
    List<T>& lst_;
    typename List<T>::Link* p_;
public:

    List_itor(List<T>& lst):lst_(lst),p_(lst.head_){
    }
    T* first(){
        p_ = lst_.head_->next;        
        return p_->data;
    }
    T* next(){
        p_ = p_->next;
        return (( p_ != NULL)? p_->data:NULL);
    }
};

int main()
{
    Vector<int> vi(10);
    vi[0] = 1;
    vi[1] = 2;
    vi[2] = 3;
    vi[3] = 4;
    vi[4] = 5;
    vi[5] = 6;
    vi[6] = 7;
    vi[7] = 8;
    vi[8] = 9;
    vi[9] = 10;
    
    Vector_itor<int> vit(vi);
    for(int* pint = vit.first(); pint != NULL; pint = vit.next())
        std::cout << *pint << " ";
    std::cout << std::endl;
    List<int> li;
    li.put(new int(1));
    li.put(new int(2));
    li.put(new int(3));    
    li.put(new int(4));
    li.put(new int(5));
    li.put(new int(6));    
    li.put(new int(7));
    li.put(new int(8));
    li.put(new int(1000));
    

    List_itor<int> lit(li);
    for(int* pint = lit.first(); pint != NULL; pint = lit.next())
        std::cout << *pint << " ";
    std::cout << std::endl;
}


    
